﻿using ConsoleApp_EXL;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET
{
    public static class GenerateHmiTags
    {
        
        public static void GenerateHmiTagsFromTable(List<InputTableRow> rows, string? path, string connection )
        {
            using (FileStream exlFile = new FileStream(path + @"/HMITags.xlsx", FileMode.Create, FileAccess.ReadWrite))
            {
                IWorkbook workBook = new XSSFWorkbook();

                ISheet sheet = workBook.CreateSheet("Hmi Tags");

                IRow row_0 = sheet.CreateRow(0);

                #region 写入表头
                row_0.CreateCell(0).SetCellValue("Name");
                row_0.CreateCell(1).SetCellValue("Path");
                row_0.CreateCell(2).SetCellValue("Connection");
                row_0.CreateCell(3).SetCellValue("PLC tag");
                row_0.CreateCell(4).SetCellValue("DataType");
                row_0.CreateCell(5).SetCellValue("Length");
                row_0.CreateCell(6).SetCellValue("Coding");
                row_0.CreateCell(7).SetCellValue("Access Method");
                row_0.CreateCell(8).SetCellValue("Address");
                row_0.CreateCell(9).SetCellValue("Indirect addressing");
                row_0.CreateCell(10).SetCellValue("Index tag");
                row_0.CreateCell(11).SetCellValue("Start value");
                row_0.CreateCell(12).SetCellValue("ID tag");
                row_0.CreateCell(13).SetCellValue("Display name [zh-CN]");
                row_0.CreateCell(14).SetCellValue("Comment [zh-CN]");
                row_0.CreateCell(15).SetCellValue("Acquisition mode");
                row_0.CreateCell(16).SetCellValue("Acquisition cycle");
                row_0.CreateCell(17).SetCellValue("Limit Upper 2 Type");
                row_0.CreateCell(18).SetCellValue("Limit Upper 2");
                row_0.CreateCell(19).SetCellValue("Limit Upper 1 Type");
                row_0.CreateCell(20).SetCellValue("Limit Upper 1");
                row_0.CreateCell(21).SetCellValue("Limit Lower 1 Type");
                row_0.CreateCell(22).SetCellValue("Limit Lower 1");
                row_0.CreateCell(23).SetCellValue("Limit Lower 2 Type");
                row_0.CreateCell(24).SetCellValue("Limit Lower 2");
                row_0.CreateCell(25).SetCellValue("Linear scaling");
                row_0.CreateCell(26).SetCellValue("End value PLC");
                row_0.CreateCell(27).SetCellValue("Start value PLC");
                row_0.CreateCell(28).SetCellValue("End value HMI");
                row_0.CreateCell(29).SetCellValue("Start value HMI");
                row_0.CreateCell(30).SetCellValue("Gmp relevant");
                row_0.CreateCell(31).SetCellValue("Confirmation Type");
                row_0.CreateCell(32).SetCellValue("Mandatory Commenting");
                #endregion

                var value_in = rows.Where(row => row.Type == "Bool" & row.InOut == "In");
                int c = value_in.ToList().Count;
                for (int i = 0; i < c; i++)
                {
                    IRow row_in = sheet.CreateRow(i + 1);
                    row_in.CreateCell(0).SetCellValue(value_in.ToList()[i].Adress);
                    row_in.CreateCell(1).SetCellValue("DI"); 
                    row_in.CreateCell(2).SetCellValue(connection);
                    row_in.CreateCell(3).SetCellValue("\"" + value_in.ToList()[i].Adress + "\"");
                    row_in.CreateCell(4).SetCellValue(value_in.ToList()[i].Type);
                    row_in.CreateCell(5).SetCellValue("1");
                    row_in.CreateCell(6).SetCellValue("Binary");
                    row_in.CreateCell(7).SetCellValue("Absolute access");
                    row_in.CreateCell(8).SetCellValue("%" + value_in.ToList()[i].Adress);
                    row_in.CreateCell(9).SetCellValue("False");
                    row_in.CreateCell(10).SetCellValue("<No Value>");
                    row_in.CreateCell(11).SetCellValue("<No Value>");
                    row_in.CreateCell(12).SetCellValue("0");
                    row_in.CreateCell(13).SetCellValue("<No Value>");
                    row_in.CreateCell(14).SetCellValue(value_in.ToList()[i].Notes);
                    row_in.CreateCell(15).SetCellValue("Cyclic in operation");
                    row_in.CreateCell(16).SetCellValue("1 s");
                    row_in.CreateCell(17).SetCellValue("None");
                    row_in.CreateCell(18).SetCellValue("<No Value>");
                    row_in.CreateCell(19).SetCellValue("None");
                    row_in.CreateCell(20).SetCellValue("<No Value>");
                    row_in.CreateCell(21).SetCellValue("None");
                    row_in.CreateCell(22).SetCellValue("<No Value>");
                    row_in.CreateCell(23).SetCellValue("None");
                    row_in.CreateCell(24).SetCellValue("<No Value>");
                    row_in.CreateCell(25).SetCellValue("False");
                    row_in.CreateCell(26).SetCellValue("10");
                    row_in.CreateCell(27).SetCellValue("0");
                    row_in.CreateCell(28).SetCellValue("100");
                    row_in.CreateCell(29).SetCellValue("0");
                    row_in.CreateCell(30).SetCellValue("False");
                    row_in.CreateCell(31).SetCellValue("None");
                    row_in.CreateCell(32).SetCellValue("False");
                }

                var value_out = rows.Where(row => row.Type == "Bool" & row.InOut == "Out");

                for (int i = 0; i < value_out.ToList().Count; i++)
                {
                    IRow row_out = sheet.CreateRow(i + 1 + value_in.ToList().Count);
                    row_out.CreateCell(0).SetCellValue(value_out.ToList()[i].Adress);
                    row_out.CreateCell(1).SetCellValue("DQ");
                    row_out.CreateCell(2).SetCellValue(connection);
                    row_out.CreateCell(3).SetCellValue("\"" + value_out.ToList()[i].Adress + "\"");
                    row_out.CreateCell(4).SetCellValue(value_out.ToList()[i].Type);
                    row_out.CreateCell(5).SetCellValue("1");
                    row_out.CreateCell(6).SetCellValue("Binary");
                    row_out.CreateCell(7).SetCellValue("Absolute access");
                    row_out.CreateCell(8).SetCellValue("%" + value_out.ToList()[i].Adress);
                    row_out.CreateCell(9).SetCellValue("False");
                    row_out.CreateCell(10).SetCellValue("<No Value>");
                    row_out.CreateCell(11).SetCellValue("<No Value>");
                    row_out.CreateCell(12).SetCellValue("0");
                    row_out.CreateCell(13).SetCellValue("<No Value>");
                    row_out.CreateCell(14).SetCellValue(value_out.ToList()[i].Notes);
                    row_out.CreateCell(15).SetCellValue("Cyclic in operation");
                    row_out.CreateCell(16).SetCellValue("1 s");
                    row_out.CreateCell(17).SetCellValue("None");
                    row_out.CreateCell(18).SetCellValue("<No Value>");
                    row_out.CreateCell(19).SetCellValue("None");
                    row_out.CreateCell(20).SetCellValue("<No Value>");
                    row_out.CreateCell(21).SetCellValue("None");
                    row_out.CreateCell(22).SetCellValue("<No Value>");
                    row_out.CreateCell(23).SetCellValue("None");
                    row_out.CreateCell(24).SetCellValue("<No Value>");
                    row_out.CreateCell(25).SetCellValue("False");
                    row_out.CreateCell(26).SetCellValue("10");
                    row_out.CreateCell(27).SetCellValue("0");
                    row_out.CreateCell(28).SetCellValue("100");
                    row_out.CreateCell(29).SetCellValue("0");
                    row_out.CreateCell(30).SetCellValue("False");
                    row_out.CreateCell(31).SetCellValue("None");
                    row_out.CreateCell(32).SetCellValue("False");
                }

                workBook.Write(exlFile, false); //写入目标表格
            }
        }
    }
    
           
}
